<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">

      <el-form-item label="订单状态" prop="orderState">
        <el-select v-model="queryParams.orderState" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in orderStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="优惠券" prop="useCoupon">
        <el-select v-model="queryParams.useCoupon" placeholder="请选择是否使用了优惠券" clearable size="small">
          <el-option
            v-for="dict in useCouponOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型 " clearable size="small">
          <el-option
            v-for="dict in orderTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="检测结果" prop="checkResult">
        <el-select v-model="queryParams.checkResult" placeholder="请选择检测结果" clearable size="small">
          <el-option
            v-for="dict in checkResultOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="支付状态" prop="isPay">
        <el-select v-model="queryParams.isPay" placeholder="请选择是否已支付" clearable size="small">
          <el-option
            v-for="dict in isPayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="gmtTime">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.gmtTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择订单创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付通道" prop="payChannel">
        <el-select v-model="queryParams.payChannel" placeholder="请选择支付通道" clearable size="small">
          <el-option
            v-for="dict in payChannelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单号" align="center" prop="orderNum"/>
      <el-table-column label="订单金额" align="center" prop="orderPrice"/>
      <el-table-column label="实际支付" align="center" prop="actPrice"/>
      <el-table-column label="检测费" align="center" prop="stationFee"/>
      <el-table-column label="代驾费" align="center" prop="driverFee"/>
      <el-table-column label="代金券" align="center" prop="couponFee"/>
      <el-table-column label="订单状态" align="center" prop="orderState" :formatter="orderStateFormat"/>
      <el-table-column label="订单备注" align="center" prop="remark"/>
      <el-table-column label="订单类型" align="center" prop="orderType" :formatter="orderTypeFormat"/>
      <el-table-column label="检测结果" align="center" prop="checkResult" :formatter="checkResultFormat"/>
      <el-table-column label="是否支付" align="center" prop="isPay" :formatter="isPayFormat"/>
      <el-table-column label="支付通道" align="center" prop="payChannel" :formatter="payChannelFormat"/>
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown"  @command="handleCommand">
              <el-dropdown-item>
                <el-button
                  type="info"
                  size="mini"
                  @click="handleChooseDriver(scope.row.id)"
                  v-hasPermi="['order:order:edit']"
                  v-if="scope.row.orderType === 2"
                >指派驾驶员
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item command="a"  v-hasPermi="['order:order:edit']">
                <el-button
                  type="info"
                  size="mini"
                  @click="handleTimeline(scope.row.id)"
                  v-hasPermi="['order:timeline:list']"
                >时  间  轴
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="info"
                  size="mini"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['order:order:edit']"
                >订单详情
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="info"
                  size="mini"
                  @click="handleImg(scope.row.id)"
                  v-hasPermi="['order:img:orderImg']"
                >订单图片
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="danger"
                  size="mini"
                  @click="handleCancel(scope.row)"
                  v-hasPermi="['order:order:cancel']"
                >订单取消
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="warning"
                  size="mini"
                  @click="handleRefund(scope.row)"
                  v-hasPermi="['order:refund:create']"
                >申请退款
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="warning"
                  size="mini"
                  @click="handleMjsl(scope.row.id)"
                  v-hasPermi="['order:order:mjsl']"
                  v-if="scope.row.orderType === 1 && scope.row.orderState === 1"
                >免检受理
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="warning"
                  size="mini"
                  @click="handleMjwj(scope.row.id)"
                  v-hasPermi="['order:order:mjsl']"
                  v-if="scope.row.orderType === 1 && scope.row.orderState === 3"
                >免检完结
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 订单详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" v-dialogdrag append-to-body>
      <order-detail :order="form" :key="(new Date().getTime())"></order-detail>
    </el-dialog>

    <!-- 时间轴对话框 -->
    <el-dialog :title="title" :visible.sync="timeopen" width="1000px" v-dialogdrag append-to-body>
      <order-timeline :orderId="orderId" :key="(new Date().getTime())"></order-timeline>
    </el-dialog>

    <!-- 订单图片对话框 -->
    <el-dialog :title="title" :visible.sync="imgopen" width="1400px" v-dialogdrag append-to-body>
      <order-img :orderId="orderId" :key="(new Date().getTime())"></order-img>
    </el-dialog>

    <!-- 指派驾驶员对话框 -->
    <el-dialog :title="title" :visible.sync="driveropen" width="1400px" v-dialogdrag append-to-body>
      <choose-driver :orderId="orderId" :key="(new Date().getTime())" @emitdriverzpDriver="emitdriverzpDriver"></choose-driver>
    </el-dialog>

    <!-- 免检办理完结对话框 -->
    <el-dialog :title="title" :visible.sync="mjwjopen" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="检测结果" prop="state">
          <el-select v-model="mjwjform.checkResult" placeholder="请选择检测结果" @change="mjwjChange" clearable size="small">
            <el-option
              v-for="dict in auditState"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="mjwjreasonshow"  label="拒绝原因" prop="state">
          <el-select v-if="!mjwjreasonipt" v-model="mjwjform.rejectReason" placeholder="请选择拒绝原因" clearable size="small">
            <el-option
              v-for="dict in reasonOps"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
          <el-input v-if="mjwjreasonipt"
            v-model="mjwjform.rejectReason"
            placeholder="请输入拒绝原因"
            clearable
            size="small"
          />
          <el-button type="text" @click="mjwjreasoniptChg">自定义/选择</el-button>
        </el-form-item>
        <el-form-item label="快递公司" prop="state">
          <el-select v-model="mjwjform.expressCompany" placeholder="请选择快递公司" clearable size="small">
            <el-option
              v-for="dict in compOps"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="gmtTime">
          <el-input
            v-model="mjwjform.expressNumber"
            placeholder="请输入订单号"
            clearable
            size="small"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="cancelmjwj">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listOrder, getOrder, exportOrder,cancelOrder,refundOrder,mjslOrder,mjwjOrder} from "@/api/order/order";
  import orderDetail from './OrderDetail'
  import orderTimeline from './OrderTimeline'
  import orderImg from './OrderImg'
  import chooseDriver from './chooseDriver'

  export default {
    name: "Order",
    props: {
      memberId: {
        type: Number,
        default: undefined
      },
      driverId: {
        type: Number,
        default: undefined
      },
      stationId: {
        type: Number,
        default: undefined
      }
    },
    components: {orderDetail, orderTimeline,orderImg,chooseDriver},
    data() {
      return {
        orderId: null,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 订单表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        timeopen: false,
        imgopen: false,
        mjwjopen: false,
        driveropen: false,
        mjwjreasonshow: false,
        mjwjreasonipt: false,
        // 订单状态字典
        orderStateOptions: [],
        // 是否使用了优惠券字典
        useCouponOptions: [],
        // 订单类型 1六年免检 2 代驾审车  3 自驾审车字典
        orderTypeOptions: [],
        // 检测结果   1合格   0不合格字典
        checkResultOptions: [],
        // 是否已支付   0为支付   1已支付字典
        isPayOptions: [],
        // 支付通道  1微信  2支付宝字典
        payChannelOptions: [],
        // 六年免检拒绝原因
        reasonOps: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          memberId: this.memberId,
          driverId: this.driverId,
          orderState: undefined,
          stationId: this.stationId,
          orderNum: undefined,
          orderPrice: undefined,
          actPrice: undefined,
          stationFee: undefined,
          driverFee: undefined,
          couponFee: undefined,
          useCoupon: undefined,
          orderType: undefined,
          driverVerfiCode: undefined,
          stationVerfiCode: undefined,
          checkResult: undefined,
          isPay: undefined,
          gmtTime: undefined,
          outTradeNo: undefined,
          payChannel: undefined
        },
        // 表单参数
        form: {},
        mjwjform: {},
        // 表单校验
        rules: {},
        auditState: [{
          label: '审核通过',
          value: '1'
        }, {
          label: '审核拒绝',
          value: '2'
        }],
        compOps: [{
          label: '顺丰快递',
          value: '顺丰快递'
        }, {
          label: '中通快递',
          value: '中通快递'
        }]
      };
    },
    created() {
      this.getList();
      this.getDicts("order_state").then(response => {
        this.orderStateOptions = response.data;
      });
      this.getDicts("sys_is_enable").then(response => {
        this.useCouponOptions = response.data;
        this.isPayOptions = response.data;
      });
      this.getDicts("order_type").then(response => {
        this.orderTypeOptions = response.data;
      });
      this.getDicts("check_result").then(response => {
        this.checkResultOptions = response.data;
      });
      this.getDicts("pay_channel").then(response => {
        this.payChannelOptions = response.data;
      });
      this.getDicts("express_company").then(response => {
        this.compOps = response.data;
      });
      this.getDicts("six_year_reject_reason").then(response => {
        this.reasonOps = response.data;
      });
    },
    methods: {
      /** 查询订单列表 */
      getList() {
        this.loading = true;
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 订单状态字典翻译
      orderStateFormat(row, column) {
        return this.selectDictLabel(this.orderStateOptions, row.orderState);
      },
      // 是否使用了优惠券字典翻译
      useCouponFormat(row, column) {
        return this.selectDictLabel(this.useCouponOptions, row.useCoupon);
      },
      // 订单类型 1六年免检 2 代驾审车  3 自驾审车字典翻译
      orderTypeFormat(row, column) {
        return this.selectDictLabel(this.orderTypeOptions, row.orderType);
      },
      // 检测结果   1合格   0不合格字典翻译
      checkResultFormat(row, column) {
        return this.selectDictLabel(this.checkResultOptions, row.checkResult);
      },
      // 是否已支付   0为支付   1已支付字典翻译
      isPayFormat(row, column) {
        return this.selectDictLabel(this.isPayOptions, row.isPay);
      },
      // 支付通道  1微信  2支付宝字典翻译
      payChannelFormat(row, column) {
        return this.selectDictLabel(this.payChannelOptions, row.payChannel);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 取消按钮
      cancelmjwj() {
        this.mjwjopen = false;
        this.reset();
      },
      emitdriverzpDriver() {
        this.driveropen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          memberId: undefined,
          driverId: undefined,
          orderState: undefined,
          stationId: undefined,
          remark: undefined,
          orderNum: undefined,
          orderPrice: undefined,
          actPrice: undefined,
          stationFee: undefined,
          driverFee: undefined,
          couponFee: undefined,
          useCoupon: undefined,
          orderType: undefined,
          driverVerfiCode: undefined,
          stationVerfiCode: undefined,
          checkResult: undefined,
          isPay: undefined,
          gmtTime: undefined,
          outTradeNo: undefined,
          payChannel: undefined
        };
        this.resetForm("form");
      },
      resetAuditForm() {
        this.mjwjform = {
          orderId: this.orderId,
          checkResult: undefined,
          expressNumber: undefined,
          expressCompany: undefined,
          rejectReason: undefined,

        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getOrder(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "订单详情";
        });
      },

      /** 免检完结按钮操作 */
      handleMjwj(id) {
        this.orderId = id;
        this.resetAuditForm();
        this.mjwjopen = true;
        this.title = "免检完结";
      },

      /** 指派驾驶员按钮操作 */
      handleChooseDriver(id) {
        this.orderId = id;
        this.driveropen = true;
        this.title = "指派驾驶员";
      },
      submitAudit: function () {
        mjwjOrder(this.mjwjform).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功");
            this.mjwjopen = false;
            this.getList();
          } else {
            this.msgError(response.msg);
          }
        });
      },
      /** 取消按钮操作 */
      handleCancel(row) {
        const id = row.id
        this.$confirm('是否确认取消该订单?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return cancelOrder(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function () {
        });
      },
      /** 免检受理按钮操作 */
      handleMjsl(id) {
        this.$confirm('是否确认受理该订单?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return mjslOrder(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function () {
        });
      },
      /** 退款按钮操作 */
      handleRefund(row) {
        const id = row.id
        this.$confirm('是否确认为该订单执行退款操作?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return refundOrder(id);
        }).then((res) => {
          this.getList();
          this.msgSuccess(res.msg);
        }).catch(function () {
        });
      },
      /** 时间轴按钮操作 */
      handleTimeline(id) {
        this.orderId = id;
        this.timeopen = true;
        this.title = "订单操作时间轴";
      },
      /** 时间轴按钮操作 */
      handleImg(id) {
        this.orderId = id;
        this.imgopen = true;
        this.title = "订单图片";
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      },
      handleCommand(command) {
        this.$message('click on item ' + command);
      },
      mjwjChange(val){
        if('2' === val){
          this.mjwjreasonshow =  true;
        }else{
          this.mjwjreasonshow =  false;
        }

      },
      mjwjreasoniptChg(){
        this.mjwjreasonipt = ! this.mjwjreasonipt;
      }
    }
  };
</script>
<style>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 6px;
  }
  .el-dropdown {
    font-size: 12px;
  }
</style>
